<template>
  <div class="password-login">
    <!-- App风格弹窗 -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>提示</h3>
        </div>
        <div class="modal-body">
          {{ modalMessage }}
        </div>
        <div class="modal-footer">
          <button class="modal-btn confirm" @click="closeModal">确定</button>
        </div>
      </div>
    </div>
    
    <!-- 状态栏 -->
    <div class="status-bar">
      <span class="time">12:00</span>
      <div class="status-icons">
        <span class="signal"></span>
        <span class="wifi"></span>
        <span class="battery"></span>
      </div>
    </div>

    <!-- 头部 -->
    <div class="header">
      <div class="close-btn" @click="goBack">×</div>
      <div class="register-link" @click="goToRegister">注册</div>
    </div>
    
    <!-- 主要内容 -->
    <div class="content">
      <!-- 标题 -->
      <div class="title-section">
        <h1 class="main-title">账号密码登录</h1>
        <p class="subtitle">使用已注册账号登录</p>
      </div>
      
      <!-- 登录表单 -->
      <div class="login-form">
        <!-- 账号输入 -->
        <div class="input-group">
          <div class="input-icon">
            <div class="user-icon"></div>
          </div>
          <input 
            type="text" 
            v-model="account" 
            placeholder="手机号/邮箱/用户名"
            class="account-input"
          >
        </div>
        
        <!-- 密码输入 -->
        <div class="input-group">
          <div class="input-icon">
            <div class="lock-icon"></div>
          </div>
          <div class="password-wrapper">
            <input 
              :type="showPassword ? 'text' : 'password'" 
              v-model="password" 
              placeholder="请输入密码"
              class="password-input"
            >
            <button 
              class="eye-btn" 
              @click="togglePassword"
              type="button"
            >
              <div :class="showPassword ? 'eye-open' : 'eye-close'"></div>
            </button>
          </div>
        </div>
        
        <!-- 忘记密码链接 -->
        <div class="forgot-password">
          <a href="#" @click="handleForgotPassword">忘记密码</a>
        </div>
      </div>
      
      <!-- 登录按钮 -->
      <div class="login-actions">
        <button 
          class="login-btn" 
          @click="handleLogin"
          :disabled="!canLogin"
        >
          登录
        </button>
        
        <div class="alternative-login">
          <a href="#" @click="goToVerificationLogin">用手机验证码登陆</a>
        </div>
      </div>
      
      <!-- 快速登录 -->
      <div class="quick-login">
        <div class="divider">
          <span class="divider-text">快速登录</span>
        </div>
        <div class="login-methods">
          <div class="method wechat" @click="handleWechatLogin">
            <div class="wechat-icon"></div>
          </div>
          <div class="method apple" @click="handleAppleLogin">
            <div class="apple-icon"></div>
          </div>
        </div>
      </div>
      
      <!-- 协议同意 -->
      <div class="agreement">
        <label class="checkbox-container">
          <input type="checkbox" v-model="agreed" class="checkbox">
          <span class="checkmark"></span>
        </label>
        <span class="agreement-text">
          我已阅读并同意
          <a href="#" class="link">《中国移动认证服务条款》</a>
          和
          <a href="#" class="link">AI智能医疗辅助系统 用户协议、隐私政策</a>
          以及
          <a href="#" class="link">儿童个人信息保护政策</a>
        </span>
      </div>
    </div>

    <!-- 底部手势条 -->
    <div class="gesture-bar"></div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

export default defineComponent({
  name: 'PasswordLogin',
  setup() {
    const router = useRouter()
    const account = ref('')
    const password = ref('')
    const showPassword = ref(false)
    const agreed = ref(false)
    const loading = ref(false)
    
    // 弹窗相关
    const showModal = ref(false)
    const modalMessage = ref('')

    // 计算属性
    const canLogin = computed(() => {
      return account.value.trim() && 
             password.value.trim() && 
             agreed.value
    })

    // 显示弹窗
    const showAppModal = (message: string) => {
      modalMessage.value = message
      showModal.value = true
    }

    // 关闭弹窗
    const closeModal = () => {
      showModal.value = false
    }

    // 切换密码显示
    const togglePassword = () => {
      showPassword.value = !showPassword.value
    }

    // 登录处理
    const handleLogin = async () => {
      if (!canLogin.value) {
        showAppModal('请填写完整信息并同意协议')
        return
      }

      try {
        loading.value = true
        // 这里可以调用后端API进行账号密码登录
        // 目前使用模拟登录
        if (account.value === '13032048286' && password.value === '123456') {
          showAppModal('登录成功')
          setTimeout(() => {
            router.push('/home')
          }, 1200)
        } else {
          showAppModal('账号或密码错误')
        }
      } catch (error: any) {
        showAppModal('登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }

    // 其他功能
    const goBack = () => {
      router.back()
    }

    const goToRegister = () => {
      router.push('/register')
    }

    const goToVerificationLogin = () => {
      router.push('/verification-login')
    }

    const handleForgotPassword = () => {
      showAppModal('忘记密码功能开发中...')
    }

    const handleWechatLogin = async () => {
    // 清除 baseURL（或设置为其他值）
    axios.defaults.baseURL = '';
    await axios.get("/api", {
        params: {
            act: "login",
            appid: "3521", //自己去官网申请
            appkey: "a010aaf9582fe1587b08b283a47ca14b", //自己去官网申请
            type: 'wx',
            redirect_uri: "http://127.0.0.1:5173/home", //回调地址,就是你登录成功(账号或扫码)之后进入到的页面
        },               //替换成你自己项目的前端路径就可以 
    }).then((res) => {
        sessionStorage.setItem('longtermToken', 'hhhhnxsm')
        // 保存 code
        window.open(res.data.url, "_self") //打开第三方登录页面
    })
  };

    const handleAppleLogin = () => {
      showAppModal('Apple登录功能开发中...')
    }

    return {
      account,
      password,
      showPassword,
      agreed,
      loading,
      canLogin,
      showModal,
      modalMessage,
      showAppModal,
      closeModal,
      togglePassword,
      handleLogin,
      goBack,
      goToRegister,
      goToVerificationLogin,
      handleForgotPassword,
      handleWechatLogin,
      handleAppleLogin
    }
  }
})
</script>

<style scoped>
.password-login {
  min-height: 100vh;
  background: #ffffff;
  position: relative;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* App风格弹窗 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  animation: fadeIn 0.2s ease-out;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 280px;
  max-width: 90vw;
  overflow: hidden;
  animation: slideUp 0.3s ease-out;
}

.modal-header {
  padding: 20px 20px 0 20px;
  text-align: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.modal-body {
  padding: 20px;
  text-align: center;
  color: #666;
  font-size: 16px;
  line-height: 1.4;
}

.modal-footer {
  padding: 0 20px 20px 20px;
  display: flex;
  justify-content: center;
}

.modal-btn {
  padding: 12px 32px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.modal-btn.confirm {
  background: #007AFF;
  color: white;
}

.modal-btn.confirm:hover {
  background: #0056CC;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { 
    opacity: 0; 
    transform: translateY(20px) scale(0.95);
  }
  to { 
    opacity: 1; 
    transform: translateY(0) scale(1);
  }
}

/* 状态栏 */
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 44px;
  background: #ffffff;
}

.time {
  font-size: 14px;
  font-weight: 600;
  color: #000;
}

.status-icons {
  display: flex;
  align-items: center;
  gap: 4px;
}

.signal, .wifi, .battery {
  width: 20px;
  height: 12px;
  background: #000;
  border-radius: 2px;
}

/* 头部 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  margin-bottom: 40px;
}

.close-btn {
  font-size: 24px;
  color: #333;
  cursor: pointer;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-link {
  color: #007AFF;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}

/* 主要内容 */
.content {
  padding: 0 20px;
}

/* 标题部分 */
.title-section {
  text-align: center;
  margin-bottom: 40px;
}

.main-title {
  font-size: 24px;
  font-weight: 600;
  color: #000;
  margin: 0 0 8px 0;
}

.subtitle {
  font-size: 14px;
  color: #8E8E93;
}

/* 登录表单 */
.login-form {
  margin-bottom: 30px;
}

.input-group {
  display: flex;
  align-items: center;
  background: #F2F2F7;
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 16px;
}

.input-icon {
  margin-right: 12px;
}

.user-icon {
  width: 20px;
  height: 20px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%238E8E93"><path d="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z"/></svg>') no-repeat center;
  background-size: contain;
}

.lock-icon {
  width: 20px;
  height: 20px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%238E8E93"><path d="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"/></svg>') no-repeat center;
  background-size: contain;
}

.account-input,
.password-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 16px;
  color: #000;
  outline: none;
}

.account-input::placeholder,
.password-input::placeholder {
  color: #8E8E93;
}

.password-wrapper {
  display: flex;
  align-items: center;
  flex: 1;
}

.eye-btn {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  margin-left: 8px;
}

.eye-close {
  width: 20px;
  height: 20px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%238E8E93"><path d="M11.83,9L15,12.16C15,12.11 15,12.05 15,12A3,3 0 0,0 12,9C11.94,9 11.89,9 11.83,9M7.53,9.8L9.08,11.35C9.03,11.56 9,11.77 9,12A3,3 0 0,0 12,15C12.22,15 12.44,14.97 12.65,14.92L14.2,16.47C13.53,16.8 12.79,17 12,17A5,5 0 0,1 7,12C7,11.21 7.2,10.47 7.53,9.8M2,4.27L3.28,3L20.72,20.44L19.44,21.71L15.73,18C14.08,19.76 11.23,20.54 7.53,18.53C4.46,16.87 2,14.69 2,12C2,10.1 3.2,8.4 5,7.27L2,4.27M7,12A5,5 0 0,1 12,7C13.04,7 13.98,7.36 14.73,7.97L7.97,14.73C7.36,13.98 7,13.04 7,12Z"/></svg>') no-repeat center;
  background-size: contain;
}

.eye-open {
  width: 20px;
  height: 20px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%238E8E93"><path d="M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9M12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17M12,4.5C7,4.5 2.73,7.61 1,12C2.73,16.39 7,19.5 12,19.5C17,19.5 21.27,16.39 23,12C21.27,7.61 17,4.5 12,4.5Z"/></svg>') no-repeat center;
  background-size: contain;
}

.forgot-password {
  text-align: right;
  margin-top: 8px;
  margin-bottom: 16px;
}

.forgot-password a {
  color: #007AFF;
  text-decoration: none;
  font-size: 14px;
}

/* 登录操作 */
.login-actions {
  margin-bottom: 40px;
}

.login-btn {
  width: 100%;
  height: 50px;
  background: #007AFF;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 16px;
  transition: background-color 0.2s;
}

.login-btn:hover:not(:disabled) {
  background: #0056CC;
}

.login-btn:disabled {
  background: #C7C7CC;
  cursor: not-allowed;
}

.alternative-login {
  text-align: center;
}

.alternative-login a {
  color: #007AFF;
  text-decoration: none;
  font-size: 14px;
}

/* 快速登录 */
.quick-login {
  margin-bottom: 40px;
}

.divider {
  position: relative;
  text-align: center;
  margin-bottom: 30px;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #E5E5EA;
}

.divider-text {
  background: #ffffff;
  padding: 0 16px;
  color: #8E8E93;
  font-size: 14px;
  position: relative;
  z-index: 1;
}

.login-methods {
  display: flex;
  justify-content: center;
  gap: 40px;
}

.method {
  width: 50px;
  height: 50px;
  border-radius: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.2s;
}

.method:hover {
  transform: scale(1.1);
}

.wechat {
  background: #07C160;
}

.apple {
  background: #000;
}

.wechat-icon {
  width: 24px;
  height: 24px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white"><path d="M8.5,13.5A1.5,1.5 0 0,1 7,12A1.5,1.5 0 0,1 8.5,10.5A1.5,1.5 0 0,1 10,12A1.5,1.5 0 0,1 8.5,13.5M15.5,13.5A1.5,1.5 0 0,1 14,12A1.5,1.5 0 0,1 15.5,10.5A1.5,1.5 0 0,1 17,12A1.5,1.5 0 0,1 15.5,13.5M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"/></svg>') no-repeat center;
  background-size: contain;
}

.apple-icon {
  width: 24px;
  height: 24px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white"><path d="M18.71,19.5C17.88,20.74 17,21.95 15.66,21.97C14.32,22 13.89,21.18 12.37,21.18C10.84,21.18 10.37,21.95 9.1,22C7.79,22.05 6.8,20.68 5.96,19.47C4.25,17 2.94,12.45 4.7,9.39C5.57,7.87 7.13,6.91 8.82,6.88C10.1,6.86 11.32,7.75 12.11,7.75C12.89,7.75 14.37,6.68 15.92,6.84C16.57,6.87 18.39,7.1 19.56,8.82C19.47,8.88 17.39,10.1 17.41,12.63C17.44,15.65 20.06,16.66 20.09,16.67C20.06,16.74 19.67,18.11 18.71,19.5M13,3.5C13.73,2.67 14.94,2.04 15.94,2C16.07,3.17 15.6,4.35 14.9,5.19C14.21,6.04 13.07,6.7 11.95,6.61C11.8,5.46 12.36,4.26 13,3.5Z"/></svg>') no-repeat center;
  background-size: contain;
}

/* 协议同意 */
.agreement {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 40px;
  padding: 0 10px;
}

.checkbox-container {
  position: relative;
  display: inline-block;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  margin-top: 2px;
}

.checkbox {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 20px;
  background-color: #fff;
  border: 2px solid #C7C7CC;
  border-radius: 4px;
  transition: all 0.2s;
}

.checkbox:checked ~ .checkmark {
  background-color: #007AFF;
  border-color: #007AFF;
}

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

.checkbox:checked ~ .checkmark:after {
  display: block;
}

.checkbox-container .checkmark:after {
  left: 6px;
  top: 2px;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.agreement-text {
  font-size: 12px;
  color: #8E8E93;
  line-height: 1.4;
}

.link {
  color: #007AFF;
  text-decoration: none;
}

.link:hover {
  text-decoration: underline;
}

/* 底部手势条 */
.gesture-bar {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 134px;
  height: 5px;
  background: #000;
  border-radius: 3px;
  margin-bottom: 8px;
}

/* 响应式设计 */
@media (max-width: 375px) {
  .main-title {
    font-size: 20px;
  }
  
  .login-methods {
    gap: 30px;
  }
  
  .method {
    width: 45px;
    height: 45px;
  }
}
</style> 